package cn.codergege.demo.interceptor

import org.springframework.web.servlet.HandlerInterceptor
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class LoginHandlerInterceptor: HandlerInterceptor {
    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        val loginUsername = request.getSession().getAttribute("loginUsername")
        if(loginUsername == null) {
            // 没有登录, 返回登录页
            request.setAttribute("msg", "请先登录")
            request.getRequestDispatcher("/login-page").forward(request, response)
            return false
        } else {
            return true
        }
    }
}